﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Text;
using CommonHelp;
using Core.Unity;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Services {
    public class AISInfoService : IInfoService {
        private EventHandler<ReceivedDataEventArgs> _ReceivedDataEvents;
        private List<AISCompanyInfo> _DataList;

        public void OnModelCreating(ref System.Data.Entity.DbModelBuilder modelBuilder) {
            modelBuilder.Entity<AISCompanyInfo>().ToTable("WEBDATA_AISCOMPANY");
            modelBuilder.Entity<AISCompanyInfo>().Ignore(a => a.Shps);
            modelBuilder.Entity<AISShipInfo>().ToTable("WEBDATA_AISSHIP");
        }

        public event EventHandler<ReceivedDataEventArgs> AfterQueryData {
            add {
                _ReceivedDataEvents += value;
            }
            remove {
                _ReceivedDataEvents -= value;
            }
        }

        public void QueryData() {
            string url = ConfigurationManager.AppSettings["QueryAISUrl"].ToString();
            double ms = DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
            url = string.Format(url, ms.ToString());

            HttpHelper http = new HttpHelper(url);
            http.GetAsync(AfterGetData);
        }

        private void AfterGetData(string responseString) {
            List<AISCompanyInfo> infos = JsonConvert.DeserializeObject<List<AISCompanyInfo>>(responseString);

            using (WebDataContext db = new WebDataContext()) {
                foreach (var item in infos) {
                    var companies = db.Set<AISCompanyInfo>();
                    var existCompany = companies.FirstOrDefault(
                        a => a.CN == item.CN
                        && a.CR == item.CR
                        && a.CI == item.CI
                        && a.TS == item.TS
                        && a.RN == item.RN
                        && a.MF == item.MF
                        && a.ALS == item.ALS
                        && a.ALE == item.ALE
                        && a.EM == item.EM
                        && a.TN == item.TN
                        && a.SHARE == item.SHARE);
                    if (existCompany != null) {
                        item.ID = existCompany.ID;
                    }
                    else {
                        item.ID = Guid.NewGuid().ToString();
                    }

                    foreach (var ship in item.Shps) {
                        ship.CompanyID = item.ID;
                    }
                }
            }

            _DataList = infos;

            if (_ReceivedDataEvents != null) {
                _ReceivedDataEvents(this, new ReceivedDataEventArgs(infos));
            }
        }

        public void SaveData(object data) {
            using (WebDataContext db = new WebDataContext()) {
                List<AISCompanyInfo> list = data as List<AISCompanyInfo>;
                DbSet<AISCompanyInfo> infos = db.Set<AISCompanyInfo>();
                foreach (var item in list) {
                    if (infos.Any(a => a.ID == item.ID)) {
                        continue;
                    }

                    infos.Add(item);
                }
                db.SaveChanges();
            }
        }
    }
}
